package cn.stronglink.assetmanage.utils.dao;

import java.util.List;

import cn.stronglink.assetmanage.MyApplication;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.RepailDetailDao;
import cn.stronglink.assetmanage.dao.RepairDao;
import cn.stronglink.assetmanage.entity.Asset;
import cn.stronglink.assetmanage.entity.Check;
import cn.stronglink.assetmanage.entity.RepailDetail;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * Author by chencongcong,Date on 2018/9/25
 */
public class RepairDataUtil {
    /**
     * 获取维修单列表  3是已维修
     *
     * @return
     */
    public static List<Repair> getDataByRepairIdDistince() {
        List<Repair> repairList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().queryBuilder().distinct()
                .orderDesc(RepairDao.Properties.CreateTime).list();
        if (repairList != null) {
            return repairList;
        }
        return null;
    }

    /**
     * 获取维修单列表  3是已维修
     *
     * @return
     */
    public static List<Repair> getDataByRepairedDistince() {
        List<Repair> repairList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().queryBuilder().distinct()
                .where(RepairDao.Properties.Status.eq(Constants.STATUS_THREE)).orderDesc(RepairDao.Properties.CreateTime).list();
        if (repairList != null) {
            return repairList;
        }
        return null;
    }

    /**
     * 获取维修单列表  4是未维修
     *
     * @return
     */
    public static List<Repair> getDataByunRepairDistince() {
        List<Repair> repairList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().queryBuilder().distinct()
                .where(RepairDao.Properties.Status.notEq(Constants.STATUS_THREE)).orderDesc(RepairDao.Properties.CreateTime).list();
        if (repairList != null) {
            return repairList;
        }
        return null;
    }


    public static Repair getRepairById(String orderNo) {
        List<Repair> repairList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().queryBuilder().distinct()
                .where(RepairDao.Properties.Id_.eq(orderNo)).list();
        return repairList == null ? null : repairList.get(0);
    }

    public static List<RepailDetail> getAllAssetByOrderNo(String orderNo) {
        List<RepailDetail> repailDetailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().queryBuilder().distinct()
                .where(RepailDetailDao.Properties.RepairId_.eq(orderNo)).list();
        return repailDetailList;
    }

    public static List<RepailDetail> getunRepairAssetByOrderNo(String orderNo) {
        List<RepailDetail> repailDetailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().queryBuilder().distinct()
                .where(RepailDetailDao.Properties.RepairId_.eq(orderNo))
                .where(RepailDetailDao.Properties.Status.notEq(Constants.STATUS_THREE)).list();
        return repailDetailList;
    }

    public static List<RepailDetail> getunRepairAssetByOrderNo(String orderNo, String location) {
        List<RepailDetail> repailDetailList = null;
        if (Constants.ALL_LOCATION.equals(location)) {
            repailDetailList = getunRepairAssetByOrderNo(orderNo);
        } else {
            repailDetailList = DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().queryBuilder().distinct()
                    .where(RepailDetailDao.Properties.RepairId_.eq(orderNo))
                    .where(RepailDetailDao.Properties.Status.notEq(Constants.STATUS_THREE))
                    .where(RepailDetailDao.Properties.RoomName.eq(location)).list();
        }
        return repailDetailList;
    }

    public static List<RepailDetail> getRepairedAssetByOrderNo(String orderNo) {
        List<RepailDetail> repailDetailList = DaoManager.
                getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().queryBuilder().distinct()
                .where(RepailDetailDao.Properties.RepairId_.eq(orderNo))
                .where(RepailDetailDao.Properties.Status.eq(Constants.STATUS_THREE)).list();
        return repailDetailList;
    }

    public static Repair getRepairData(Repair repair) {
        if (repair != null) {
            List<RepailDetail> repailDetailList = getAllAssetByOrderNo(repair.getId_());
            repair.setDetailList(repailDetailList);
        }
        return repair;
    }

    public static List<RepailDetail> getRepairedAssetByOrderNo(String orderNo, String location) {
        List<RepailDetail> repailDetailList = null;
        if (Constants.ALL_LOCATION.equals(location)) {
            repailDetailList = getRepairedAssetByOrderNo(orderNo);
        } else {
            repailDetailList = DaoManager.
                    getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().queryBuilder().distinct()
                    .where(RepailDetailDao.Properties.RepairId_.eq(orderNo))
                    .where(RepailDetailDao.Properties.Status.eq(Constants.STATUS_THREE))
                    .where(RepailDetailDao.Properties.RoomName.eq(location)).list();
        }

        return repailDetailList;
    }

    public static void saveRepairDetailata(Repair repair) {
        DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRepairDao().update(repair);
        // 获取详情
        if (repair.getDetailList() != null && repair.getDetailList().size() > 0) {
            for (RepailDetail detail : repair.getDetailList()) {
                detail.setRepairId_(repair.getId_());
                DaoManager.getDaoSession(MyApplication.APP_CONTEXT).getRepailDetailDao().insertOrReplace(detail);
            }
        }
    }
}
